package com.mindata.blockchain.socket.raft.server;

import com.github.wenweihu86.rpc.client.RPCClient;
import com.github.wenweihu86.rpc.client.RPCProxy;
import com.mindata.blockchain.common.RaftConf;

/**
 * @Author:wenhui
 * @Description:
 * @Date:Create in 2018-7-25 19:48
 **/
public class RaftServerServiceContainer {
    private static RaftServerService localRaftServerService = null;

    public static RaftServerService getInstance() {
        if (localRaftServerService == null) {
            synchronized (RaftServerService.class) {
                if (localRaftServerService == null) {
                    String ipPort = getIpPort();
                    RPCClient rpcClient = new RPCClient(ipPort);
                    localRaftServerService = RPCProxy.getProxy(rpcClient, RaftServerService.class);
                }
            }
        }
        return localRaftServerService;
    }

    private static String getIpPort() {
        String[] splitServer = RaftConf.localServer.split(":");
        String host = splitServer[0];
        Integer port = Integer.parseInt(splitServer[1]);
        return host + ":" + port;
    }
}
